1989D - Smithing Skill - CodeForces Solution


brute force data structures dp greedy math sortings two pointers *1900

Please click on ads to support us..

Python Code:

import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s); sys.stdout.write('\n')
def wi(n): sys.stdout.write(str(n)); sys.stdout.write('\n')
def wia(a, sep=' '): sys.stdout.write(sep.join([str(x) for x in a])); sys.stdout.write('\n')


def solve(n, m, a, b, c):
    max_a = max(a)
    M = max_a + 1

    best = [M] * M
    for i in range(n):
        diff = a[i] - b[i]
        best[a[i]] = min(best[a[i]], diff)
        
    ans = [0] * M
    waste = M
    for i in range(M):
        waste = min(waste, best[i])
        if waste < M:
            ans[i] = 2 + ans[i - waste]

    res = 0
    for ci in c:
        if ci < M:
            res += ans[ci]
        else:
            v = 1 + (ci - M) // waste
            ci -= v * waste
            res += ans[ci] + 2 * v

    return res


def main():
    n, m = ria()
    a = ria()
    b = ria()
    c = ria()
    wi(solve(n, m, a, b, c))


if __name__ == '__main__':
    main()


Comments

Submit
0 Comments
More Questions

1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon